package cn.example.networktest.utils;


import android.text.TextUtils;
import android.util.Log;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class XmlUtils {

    /**
     * <apps>
     * <app>
     * <id>1</id>
     * <name>Google Maps</name>
     * <version>1.0</version>
     * </app>
     * <app>
     * <id>2</id>
     * <name>Chrome</name>
     * <version>2.1</version>
     * </app>
     * <app>
     * <id>3</id>
     * <name>Google Play</name>
     * <version>2.3</version>
     * </app>
     * </apps>
     *
     * @param xml
     * @throws XmlPullParserException
     * @see  是从上到下的解析过程,有几个关建点
     *
     */

    public static void parseXMLWithSAX(final String xml) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        XMLReader reader = factory.newSAXParser().getXMLReader();
        reader.setContentHandler(new ContentHandler());
        try {
            reader.parse(xml);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

    }

    public static void parseXMLWithPull(String xml){

        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser pullParser = factory.newPullParser();
            pullParser.setInput(new StringReader(xml));

            int eventType = pullParser.getEventType();

            String id="";
            String name="";
            String version="";
            while (eventType!=XmlPullParser.END_DOCUMENT) {
                String nodeName = pullParser.getName();
                switch (eventType){
                    case  XmlPullParser.START_TAG:
                        if ("id".equals(nodeName)){
                            id=pullParser.nextText();
                        }
                        if (TextUtils.equals("name",nodeName)){
                            name=pullParser.nextText();
                        }
                        if (TextUtils.equals("version",nodeName)){
                            name=pullParser.nextText();
                        }
                            break;
                    case XmlPullParser.END_TAG:
                        if ("app".equals(nodeName)) {
                            Log.d("MainActivity", "id is " + id);
                            Log.d("MainActivity", "name is " + name);
                            Log.d("MainActivity", "version is " + version);
                        }
                        break;
                }

                //下一次标签
                eventType=pullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
